邻接表存储的无向图非递归深度优先遍历算法

#include <iostream>
#include <stack>

using namespace std;

#define MVNum 100

typedef int OtherInfo;
typedef string VerTexType;

typedef struct ArcNode
{
    int adjvex;
    struct ArcNode * nextarc;
    OtherInfo info;
}ArcNode;

typedef struct VNode
{
    VerTexType data;
    ArcNode *firstarc;
}VNode,AdjList[MVNum];

typedef struct
{
    AdjList vertices;
    int vexnum,arcnum;
}ALGraph;


int LocateVex(ALGraph G,VerTexType v)
{
    int i;
    for(i=0;i<G.vexnum;i++)
    {
        if(G.vertices[i].data==v) return i;
    }
    return -1;
}

int CreateUDG(ALGraph &G)
{
    int i,j,k;
    VerTexType v1,v2;
    ArcNode *p,*p2;
    cin>>G.vexnum>>G.arcnum;
    for(i=0;i<G.vexnum;i++)
    {
        cin>>G.vertices[i].data;
        G.vertices[i].firstarc=NULL;
    }
    for(k=0;k<G.arcnum;k++)
    {
        cin>>v1>>v2;
        i=LocateVex(G,v1);
        j=LocateVex(G,v2);
        p=new ArcNode;
        p->adjvex=j;
        p->nextarc=G.vertices[i].firstarc;
        G.vertices[i].firstarc=p;
        p2=new ArcNode;
        p2->adjvex=i;
        p2->nextarc=G.vertices[j].firstarc;
        G.vertices[j].firstarc=p2;
    }
    return 1;
}

void DFS_AL2(ALGraph G)
{
    bool visited[MVNum]={false};
    stack<int> S;
    cout<<G.vertices[0].data<<" ";
    visited[0]=true;
    S.push(0);
    ArcNode *p;
    while(!S.empty())
    {
        p=G.vertices[S.top()].firstarc;
        while(p&&visited[p->adjvex])//选择
        {
            p=p->nextarc;
        }
        while(p&&!visited[p->adjvex])//走这个方向
        {
            cout<<G.vertices[p->adjvex].data<<" ";
            visited[p->adjvex]=true;
            S.push(p->adjvex);
            p=G.vertices[p->adjvex].firstarc;
        }
        if(p==NULL)
        {
            S.pop();
        }
    }
}

int main()
{
    ALGraph G;
    CreateUDG(G);
    DFS_AL2(G);
}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 以下是邻接表非递归深度优先遍历的C语言代码: ```c #include<stdio.h> #include<stdlib.h> #define MAX_VERTEX_NUM 20 // 最大顶点数 #define MAX_STACK_SIZE 20 // 最大栈深度 typedef int VertexType; // 顶点的数据类型 // 定义边表结构体 typedef struct ArcNode { int adjvex; // 邻接点在顶点数组中的下标 struct ArcNode *nextarc; // 指向下一个邻接点的指针 } ArcNode; // 定义顶点表结构体 typedef struct { VertexType data; // 顶点的数据 ArcNode *firstarc; // 指向第一个邻接点的指针 } VNode; // 定义邻接表结构体 typedef struct { VNode vertex[MAX_VERTEX_NUM]; // 顶点数组 int vexnum, arcnum; // 顶点数和边数 } ALGraph; // 初始化邻接表 void init(ALGraph *G) { int i; G->vexnum = G->arcnum = 0; for (i = 0; i < MAX_VERTEX_NUM; i++) { G->vertex[i].firstarc = NULL; } } // 添加边 void addEdge(ALGraph *G, int i, int j) { ArcNode *p = (ArcNode *)malloc(sizeof(ArcNode)); p->adjvex = j; p->nextarc = G->vertex[i].firstarc; G->vertex[i].firstarc = p; G->arcnum++; } // 非递归深度优先遍历 void DFS(ALGraph *G, int v) { int visited[MAX_VERTEX_NUM]; int top = -1; // 栈顶指针 int stack[MAX_STACK_SIZE]; // 栈 ArcNode *p; int i; // 初始化visited数组 for (i = 0; i < MAX_VERTEX_NUM; i++) { visited[i] = 0; } // 将起点v入栈并标记为已访问 stack[++top] = v; visited[v] = 1; // 遍历栈中的元素 while (top != -1) { // 取出栈顶元素 i = stack[top--]; printf("%d ", G->vertex[i].data); // 遍历邻接点 p = G->vertex[i].firstarc; while (p != NULL) { // 如果邻接点未被访问,则入栈并标记为已访问 if (visited[p->adjvex] == 0) { stack[++top] = p->adjvex; visited[p->adjvex] = 1; } p = p->nextarc; } } } int main() { ALGraph G; init(&G); addEdge(&G, 0, 1); addEdge(&G, 0, 2); addEdge(&G, 1, 2); addEdge(&G, 2, 0); addEdge(&G, 2, 3); addEdge(&G, 3, 3); printf("非递归深度优先遍历结果:"); DFS(&G, 2); return 0; } ``` ### 回答2: 下面是邻接表非递归深度优先遍历的C语言代码。首先,我们需要定义一个的结构体,包含顶点数量和邻接表数组。 ```c #include <stdio.h> #include <stdlib.h> // 定义的结构体 typedef struct GraphNode{ int vertex; struct GraphNode* next; } GraphNode; typedef struct Graph{ int vertices; GraphNode** adjList; } Graph; // 创建的函数 Graph* createGraph(int vertices){ Graph* graph = (Graph*) malloc(sizeof(Graph)); graph->vertices = vertices; graph->adjList = (GraphNode**) malloc(vertices * sizeof(GraphNode*)); for(int i = 0; i < vertices; i++){ graph->adjList[i] = NULL; } return graph; } // 添加边的函数 void addEdge(Graph* graph, int src, int dest){ GraphNode* newNode = (GraphNode*) malloc(sizeof(GraphNode)); newNode->vertex = dest; newNode->next = graph->adjList[src]; graph->adjList[src] = newNode; // 若为无向图,则也需添加反向边 newNode = (GraphNode*) malloc(sizeof(GraphNode)); newNode->vertex = src; newNode->next = graph->adjList[dest]; graph->adjList[dest] = newNode; } // 深度优先遍历函数 void DFS(Graph* graph, int startVertex){ int* visited = (int*) malloc(graph->vertices * sizeof(int)); for(int i = 0; i < graph->vertices; i++){ visited[i] = 0; } // 使用栈来实现非递归遍历 int stack[graph->vertices]; int top = -1; stack[++top] = startVertex; visited[startVertex] = 1; while(top != -1){ int currentVertex = stack[top--]; printf("%d ", currentVertex); GraphNode* temp = graph->adjList[currentVertex]; while(temp){ int adjVertex = temp->vertex; if(!visited[adjVertex]){ stack[++top] = adjVertex; visited[adjVertex] = 1; } temp = temp->next; } } } // 主函数 int main(){ int vertices = 6; Graph* graph = createGraph(vertices); addEdge(graph, 0, 1); addEdge(graph, 0, 2); addEdge(graph, 1, 3); addEdge(graph, 1, 4); addEdge(graph, 2, 4); addEdge(graph, 3, 4); addEdge(graph, 3, 5); printf("深度优先遍历结果: "); DFS(graph, 0); return 0; } ``` 代码中的`createGraph`函数用于创建具有指定数量顶点的。`addEdge`函数用于在两个顶点之间添加一条边。`DFS`函数用于执行深度优先遍历。在`main`函数中,我们创建了一个具有6个顶点的,并添加了一些边。最后,我们调用`DFS`函数从第一个顶点开始进行深度优先遍历,并打印结果。 ### 回答3: 深度优先遍历(Depth First Search,简称DFS)是的一种遍历算法,其中邻接表是一种的表示方式。下面是一个非递归深度优先遍历的C语言代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_VERTEX_NUM 100 // 定义邻接表节点 typedef struct ArcNode { int adjvex; // 邻接顶点的下标 struct ArcNode *nextarc; // 指向下一个邻接表节点的指针 } ArcNode; // 定义的顶点节点 typedef struct VNode { int data; // 顶点的数据 ArcNode *firstarc; // 指向第一个邻接表节点的指针 } VNode, AdjList[MAX_VERTEX_NUM]; // 定义 typedef struct { AdjList vertices; // 顶点数组 int vexnum; // 顶点数 } Graph; // 非递归DFS遍历 void DFS(Graph G, int v) { int visited[MAX_VERTEX_NUM] = {0}; // 记录顶点是否被访问过的数组 ArcNode *stack[MAX_VERTEX_NUM]; // 用数组实现栈 int top = -1; // 栈顶指针 int i; printf("DFS遍历顺序: "); stack[++top] = &(G.vertices[v]); // 将起始顶点的邻接表节点入栈 visited[v] = 1; // 标记起始顶点已经被访问 while (top != -1) { // 栈不为空时 ArcNode *p = stack[top--]; // 弹出栈顶元素 printf("%d ", p->data); // 访问该顶点 for (ArcNode *q = p->firstarc; q != NULL; q = q->nextarc) { // 遍历该顶点的邻接点 if (visited[q->adjvex] == 0) { // 若邻接点未被访问过 stack[++top] = &(G.vertices[q->adjvex]); // 入栈 visited[q->adjvex] = 1; // 标记已访问 } } } } int main() { Graph G; G.vexnum = 4; // 假设有4个顶点 for (int i = 0; i < G.vexnum; i++) { G.vertices[i].data = i; // 顶点数据 G.vertices[i].firstarc = NULL; // 初始化邻接表节点指针为空 } // 添加边 ArcNode *arc0 = (ArcNode *)malloc(sizeof(ArcNode)); arc0->adjvex = 1; arc0->nextarc = NULL; G.vertices[0].firstarc = arc0; ArcNode *arc1 = (ArcNode *)malloc(sizeof(ArcNode)); arc1->adjvex = 2; arc1->nextarc = NULL; G.vertices[1].firstarc = arc1; ArcNode *arc2 = (ArcNode *)malloc(sizeof(ArcNode)); arc2->adjvex = 3; arc2->nextarc = NULL; G.vertices[2].firstarc = arc2; // 顶点0的邻接点为1 // 顶点1的邻接点为2 // 顶点2的邻接点为3 DFS(G, 0); return 0; } ``` 此代码采用邻接表的形式存储,并使用非递归深度优先遍历算法遍历中的顶点和边。首先定义了邻接表节点和顶点节点的数据结构,然后定义了的数据结构,包括顶点数组和顶点数。接下来,使用非递归深度优先遍历算法实现了DFS()函数,在DFS()函数中,使用栈来记录待访问的节点,并使用visited数组来标记节点是否被访问过。最后,在主函数中创建了一个具有4个顶点的,并添加了相应的边,然后调用DFS()函数进行遍历
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值